package com.example.vue.account.domain;

import com.example.vue.account.util.AccountUtil;
import com.example.vue.comm.domain.ResultData;
import com.example.vue.comm.util.StringUtil;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class Account {

    /**
     * 用户编号 - 系统生成 - 唯一性
     */
    private String accountNo;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 盐
     */
    private String salt;
    /**
     * 性别 1-男，2-女，0-未知
     */
    private Integer gender;
    /**
     * 电话号码
     */
    private String phone;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 修改时间
     */
    private Date updateTime;
    /**
     * 状态 0-初始状态 1-禁用，2-启用，3-限制登录，4-注销
     */
    private Integer status;
    /**
     * 限制登录的到期日期
     */
    private Date limitDate;
    /**
     * 用户图片
     */
    private String accountImg;

    public Account() {
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getLimitDate() {
        return limitDate;
    }

    public void setLimitDate(Date limitDate) {
        this.limitDate = limitDate;
    }

    public String getAccountImg() {
        return accountImg;
    }

    public void setAccountImg(String accountImg) {
        this.accountImg = accountImg;
    }

    /**
     * 用户注册，构建Account对象信息
     *
     * @param map
     * @return
     */
    public static ResultData<Account> reg(Map<String, String> map) throws Exception {
        ResultData<Account> data = new ResultData<>();
        String email = map.get("email");
        String pass = map.get("password");
        String passAagin = map.get("passwordAgain");
        String salt = AccountUtil.getSalt(6);
        /**
         * 校验数据合法性，如果不合法，那么返回400错误信息，并给出错误提示信息
         */
        if (Objects.isNull(pass) && pass.equals(passAagin)) {
            data.setCode(400);
            data.setMessage("密码不一致");
            return data;
        }
        Account account = new Account();
        // 暂时使用uuid做用户编号
        account.setAccountNo(UUID.randomUUID().toString().replace("-", ""));
        account.setSalt(salt);
        account.setEmail(email);
        account.setPassword(StringUtil.MD5(pass, salt));
        account.setStatus(0);
        account.setCreateTime(new Date());
        account.setGender(1);
        account.setAccountImg("默认图片地址");
        data.setData(account);
        data.setCode(200);
        return data;
    }

}
